<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="数据分组" prop="statType">
        <el-select
          v-model="queryParams.statType"
          placeholder="数据分组"
          clearable
          style="width: 120px">
          <el-option
            v-for="dict in statTypeOptions"
            :key="dict.id"
            :label="dict.text"
            :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="预算类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="预算类型"
          clearable
          style="width: 120px">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.id"
            :label="dict.text"
            :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="预算周期" prop="period">
        <el-select
          v-model="queryParams.period"
          placeholder="预算周期"
          clearable
          style="width: 120px">
          <el-option
            v-for="dict in periodOptions"
            :key="dict.id"
            :label="dict.text"
            :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="TrendCharts" @click="handleQuery" v-hasPermi="['fund:budget:stat']">统计</el-button>
        <el-button icon="refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--图表数据-->
    <div ref="statChart" :style="{height:height,margin:0 }" />

  </div>
</template>

<script setup name="BudgetStat">
  import { getBudgetStat } from "@/api/fund/budget";
  import * as echarts from 'echarts';
  import { createChart, createPieChartOption } from "@/utils/mulanbay_echarts";

  const { proxy } = getCurrentInstance();
  //图形实例
  const statChart = ref(null);
  //echarts实例
  let statChartIns = ref(null);
  const height = ref((document.body.clientHeight - 240).toString() + 'px');

  const statTypeOptions = ref([]);
  const typeOptions = ref([]);
  const periodOptions = ref([]);

  const data = reactive({
    queryParams: {
      statType: 'NAME'
    }
  });

  const { queryParams } = toRefs(data);

  /** 下拉框加载 */
  function loadOptions() {
    proxy.getEnumDict('BudgetStatType', 'FIELD', false).then(response => {
      statTypeOptions.value = response;
    });
    proxy.getEnumDict('BudgetType', 'FIELD', false).then(response => {
      typeOptions.value = response;
    });
    proxy.getEnumDict('PeriodType', 'FIELD', false).then(response => {
      periodOptions.value = response;
    });
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    initChart();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    initChart();
  }

  function initChart() {
    proxy.$modal.loading("正在加载数据，请稍候！");
    getBudgetStat(queryParams.value).then(
      response => {
        proxy.$modal.closeLoading();
        //组装chart数据
        let option = createPieChartOption(response);;
        createChart(option, statChartIns);
      }
    );
  }

  /** 初始化 **/
  onMounted(() => {
    statChartIns = echarts.init(statChart.value, "macarons");
    initChart();
    loadOptions();
  })
</script>